package com.rzx.mybackend.controller.user;

import com.rzx.mybackend.common.Result;
import com.rzx.mybackend.dto.UserRegisterDTO;
import com.rzx.mybackend.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * UserController
 *
 * @author renzhexian
 * @date 2025/7/16
 */

@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {

    private final UserService userService;

    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDTO userRegisterDTO) {
        if (userRegisterDTO.getUserName() == null || userRegisterDTO.getUserName().isEmpty() ||
                userRegisterDTO.getPassword() == null || userRegisterDTO.getPassword().isEmpty()) {
            throw new RuntimeException("用户名或密码不能为空!");
        }
        if (userRegisterDTO.getNickName() == null || userRegisterDTO.getNickName().isEmpty()) {
            throw new RuntimeException("昵称不能为空!");
        }
        if (userService.selectUserNameByUserName(userRegisterDTO.getUserName())>0){
            throw new RuntimeException("用户名已存在，请更换用户名!");
        }

        userService.register(userRegisterDTO);

        return Result.success("注册成功,请返回登录");
    }
}
